import App from './App'

import { createSSRApp } from 'vue'
import { createPinia } from 'pinia'
// 引入全局配置
import $mConfig from '@/config/index.config.js';
// 引入全局方法
import http from '@/utils/request/index.js';
import newHttp from '@/utils/request/newRequest.js';
import setHttp from '@/utils/request/setRequest.js';
import $mHelper from '@/utils/helper';
import $mRouter from '@/utils/router';
import fuiConfig from './components/firstui/fui-config'
import ZPInterceptor from '@/uni_modules/z-paging/components/z-paging/js/z-paging-interceptor'
import utils from '@/components/firstui/fui-utils';
uni.$fui = fuiConfig; 
   import {useShopStore} from '@/store/shop.js';
   import {useUserStore} from '@/store/user.js';
   import SnHeader from '@/components/sn-header.vue';
   import AkLoading from '@/components/ak-loading/index.vue';
   import uView from './uni_modules/vk-uview-ui';
export function createApp() {
  const app = createSSRApp(App);
  app.config.compilerOptions.isCustomElement = (tag) => tag.startsWith('fui-');
  app.config.globalProperties.$fui = utils;
  app.config.globalProperties.textColor = '#333333';
  app.config.globalProperties.badgeColor =  '#FF551A';
  app.config.globalProperties.bgColor = '#F8F8F8';
  app.config.globalProperties.themeColor = '#FFBB1A';
  app.config.globalProperties.greyColor = '#999999';
  app.config.globalProperties.infoColor = '#666666';
  app.config.globalProperties.linearGradient = 'linear-gradient(-90deg, #FFBB1A, #FFBB1A)';
  app.config.globalProperties.defaultLogo = 'https://cdn.shan-niang.com/shops/mendiantouxiang.png';
  app.config.globalProperties.statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
  // // 资源地址
  app.config.globalProperties.baseUrl = $mConfig.baseUrl;
  app.config.globalProperties.resourcesUrl = $mConfig.resourcesUrl;
  app.config.globalProperties.resourcesUrlZip = $mConfig.resourcesUrlZip;
  app.config.globalProperties.$http = http;
  app.config.globalProperties.$newHttp = newHttp;
  app.config.globalProperties.$setHttp = setHttp;
  app.config.globalProperties.$mHelper = $mHelper;
  app.config.globalProperties.$mRouter = $mRouter;
  app.config.globalProperties.loadError = function(msg){		   
  		this.$fui.toast(msg);
  		clearTimeout(this.loadErrorTimer);
  		this.loadErrorTimer = setTimeout(function(){
  			uni.navigateBack();
  		},1500);
  	};
  	app.config.globalProperties.getBtStyle = function(type = 'default', width, height, fontSize = 28, borderColor = this.textColor) {
  	 		let obj = {}
  	 		if (type == 'default') {
  	 			obj = {
  	 				backgroundColor: this.themeColor,
  	 				color: this.textColor,
  	 				width: width + 'rpx',
  	 				height: height + 'rpx',
  	 				fontSize: fontSize + 'rpx',
  	 				border: 0
  	 			}
  	 		} else {
  	 			obj = {
  	 				color: borderColor,
  	 				width: width + 'rpx',
  	 				height: height + 'rpx',
  	 				fontSize: fontSize + 'rpx',
  	 				border: '2rpx solid ' + borderColor,
  	 			}
  	 		}
  	 		return obj
  	 }	
  	  app.config.globalProperties.switchShop = async function(shopId, pagePath,type) {
  	 		  const shopStore = useShopStore();
  	 		  await shopStore.switchShop(shopId)
  	 		  const userStore = useUserStore();
  	 		  const tabbarIndex = userStore.tabbar.findIndex(item => {
  	 			  return item.pagePath == pagePath
  	 		  })
  	 		  if (tabbarIndex == -1) {
  	 			  if(pagePath == '/pages/cart/index'){
  	 				 $mRouter.reLaunch({ route: '/pages/cart/index' })
  	 			  }else{
  	 				 $mRouter.reLaunch({ route: '/pages/index/index' })
  	 			  }
  	 		  } else {
  	 			  $mRouter.reLaunch({ route: pagePath })
  	 		  }
  	 };
  	 app.config.globalProperties.toAlipay=function(dom) {
  	 	$mRouter.push({ route: '/pages/public/alipay?dom='+dom })
  	 };
	 app.config.globalProperties.toKefu=function(dom) {
	 	uni.showModal({
	 		title: '客服电话',
	 		content: '010-65575199',
	 		showCancel: false
	 	})
	 };
  	 app.config.globalProperties.toScanLogin=function(option) {
  	 	$mRouter.push({ route: '/pages/public/loginscan?option='+option })
  	 };
  	 app.config.globalProperties.pageBack = function(url) {
  	 		 if(url){
  	 			 $mRouter.redirectTo({
  	 			 	route: url
  	 			 })
  	 		 }else{
  	 			const pages = getCurrentPages();
  	 			if(pages.length === 1){
  	 				uni.reLaunch({
  	 					url:'/pages/index/index'
  	 				});
  	 			}else{
  	 				uni.navigateBack();
  	 			} 
  	 		 }
  	 }
app.component('sn-header', SnHeader);
app.component('akLoading', AkLoading);
const pinia = createPinia()
  app.use(pinia);
  app.use(uView)
  return {
    app
  }
}

